package cn.alcatraz.system.service.impl;

import cn.alcatraz.system.domain.Role;
import cn.alcatraz.system.mapper.RoleMapper;
import cn.alcatraz.system.service.IRoleService;
import cn.alcatraz.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author alcatraz
 * @since 2022-07-11
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void save(Role role) {
        super.save(role);
    }


    /**
     * @Title: update
     * @Description: 修改角色
     * @Author: Alcatraz
     * @Version: 1.0
     * @Date:  2022/7/13 11:40
     * @Parameters: [role]
     * @Return void
     */
    @Override
    public void update(Role role) {
        roleMapper.update(role);
        // 初始化关联表
        initRelationTable(role);
    }

    /**
     * @Title: initRelationTable
     * @Description: 初始化关联表
     * @Author: Alcatraz
     * @Version: 1.0
     * @Date:  2022/7/13 11:42
     * @Parameters: [role]
     * @Return void
     */
    public void initRelationTable(Role role){
        // 1.根据roleId删除角色菜单和角色权限关联表对应数据
        roleMapper.deleteRoleMenu(role.getId());
        roleMapper.deleteRolePermission(role.getId());
        // 2.根据roleId和菜单Id和权限Id进行新增
        roleMapper.saveRoleMenu(role.getId(), role.getMenus());
        roleMapper.saveRolePermission(role.getId(), role.getPermissions());
    }

    @Override
    public void delete(Long id) {
        super.delete(id);
    }
}
